package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Question;
import com.epam.qanda.db.model.QuestionCategory;

public class QuestionCategoryDao implements BaseDao {
	private final String SELECT_BY_CATEGORIES = "SELECT q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,COUNT(a.question_id) "
			+ "AS amount_answers FROM question q JOIN category c ON q.category_id=c.id LEFT JOIN answer a "
			+ "ON q.id = a.question_id JOIN user u ON q.user_id = u.id  AND c.name=? GROUP BY q.id ORDER BY q.time DESC";
	private final String SELECT_BY_QID = "SELECT q.id,q.user_id,u.login, u.rating, u.status, u.photo,q.category_id,q.text,q.title,q.time,q.likes,c.name,q.category_id,COUNT(a.question_id)"
			+ " AS amount_answers FROM question q JOIN category c ON q.category_id=c.id JOIN answer a "
			+ "ON q.id = a.question_id JOIN user u ON q.user_id = u.id  AND q.id=?";
	private final String SELECT_UNANSWERED = "SELECT q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,COUNT(a.question_id) AS amount_answers"
			+ " FROM question q LEFT JOIN answer a ON q.id = a.question_id JOIN category c ON q.category_id=c.id JOIN user u "
			+ "ON q.user_id = u.id WHERE a.question_id IS NULL GROUP BY q.id ORDER BY q.time DESC";
	private final String SELECT_TOP = "SELECT  q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,"
			+ "COUNT(a.question_id) AS amount_answers FROM question q LEFT JOIN answer a ON q.id = a.question_id JOIN category c ON q.category_id=c.id JOIN user u "
			+ "ON q.user_id = u.id GROUP BY q.id ORDER BY q.likes DESC,q.time";
	private final String SELECT_USER_QUESTIONS = "SELECT q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,COUNT(a.question_id) "
			+ "AS amount_answers FROM question q JOIN category c ON q.category_id=c.id LEFT JOIN answer a "
			+ "ON q.id = a.question_id JOIN user u ON q.user_id = u.id  AND q.user_id=? GROUP BY q.id ORDER BY q.time DESC";
	private final String SELECT_USER_ANSWERS = "SELECT q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,COUNT(a.question_id) "
			+ "AS amount_answers FROM question q JOIN category c ON q.category_id=c.id LEFT JOIN answer a "
			+ "ON q.id = a.question_id JOIN user u ON q.user_id = u.id  AND a.user_id=? GROUP BY q.id ORDER BY q.time DESC";
	private final String SELECT_ALL_QUESTIONS = "SELECT q.id,q.user_id,u.login, u.rating, u.status,q.category_id,q.text,q.title,q.time,q.likes,c.name,COUNT(a.question_id) "
			+ "AS amount_answers FROM question q JOIN category c ON q.category_id=c.id LEFT JOIN answer a ON q.id = a.question_id JOIN user u "
			+ "ON q.user_id = u.id GROUP BY q.id ORDER BY time DESC";
	private final String SELECT_QUESTION_FOR_24H = "SELECT * FROM question where category_id = ? and time >= SYSDATE() - INTERVAL 1 DAY";
	private static final Logger LOG = Logger.getLogger("stdout");

	@SuppressWarnings({ "unchecked" })
	public <T> T getByCategory(String name) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_BY_CATEGORIES)) {
			statement.setString(1, name);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings("unchecked")
	public <T> T getByQuestionId(int qId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		QuestionCategory questionCategory = new QuestionCategory();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_BY_QID)) {
			statement.setInt(1, qId);
			ResultSet rs = statement.executeQuery();
			if (rs.next()) {
				questionCategory = Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class);
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questionCategory;
	}

	@SuppressWarnings({ "unchecked" })
	public <T> T getUnanswered() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_UNANSWERED)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings({ "unchecked" })
	public <T> T getTop() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_TOP)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings({ "unchecked" })
	public <T> T getUserQuestions(Integer userId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_USER_QUESTIONS)) {
			statement.setInt(1, userId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings({ "unchecked" })
	public <T> T getUserAnswers(Integer userId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_USER_ANSWERS)) {
			statement.setInt(1, userId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings({ "unchecked" })
	public <T> T getAllQuestions() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<QuestionCategory> questions = new ArrayList<QuestionCategory>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_ALL_QUESTIONS)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<QuestionCategory> fromResaultSetToObject(rs,
						QuestionCategory.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@SuppressWarnings("unchecked")
	public <T> T getQuestionFor24hByIdCategories(int categoriesId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Question> questions = new ArrayList<Question>();
		try (PreparedStatement statement = connection.prepareStatement(SELECT_QUESTION_FOR_24H)) {
			statement.setInt(1, categoriesId);
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				questions.add(Converter.<Question> fromResaultSetToObject(rs, Question.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) questions;
	}

	@Override
	public <T> void create(T element) {
	}

	@Override
	public <T> T read(Integer id) {
		return null;
	}

	@Override
	public void delete(Integer id) {
	}

	@Override
	public <T> void update(T element) {
	}

}
